﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBBStoreClient
{
    public partial class frmExportProductsData : Form
    {
        UnityProcessor proc = new UnityProcessor();
        
        Threader t = new Threader();
        string _filename = "";
        float prog = 0f;
        Timer tmr = new Timer();
        public frmExportProductsData(string filename)
        {
            _filename = filename;
            InitializeComponent();
         
            this.Shown += new EventHandler(frmExportProductsData_Shown);
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);

            tmr.Interval = 1000;
            tmr.Enabled = false;
            tmr.Tick += new EventHandler(tmr_Tick);
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        void frmExportProductsData_Shown(object sender, EventArgs e)
        {
            UnityProcessor.Settings.APP_ID = "MBB Store Client";
            UnityProcessor.Settings.APP_Name = "MBB Store Client";
            UnityProcessor.Settings.DBPU_Server = StoreClientModule.Common.lServer;
            UnityProcessor.Settings.DBPU_Database = StoreClientModule.Common.lDatabase;
            UnityProcessor.Settings.DBPU_UID = StoreClientModule.Common.lUID;
            UnityProcessor.Settings.DBPU_PWD = StoreClientModule.Common.lPWD;

            prog = 1f;
            Progress();
            UnityScript getProd = new UnityScript("GETPROD", 5);
            getProd.Script_Add(Script.DBPU_FETCH, "SELECT fID, fBarcode, fDescription, fSupplier, fUnit, fContent, fStamp FROM tblproducts ORDER BY fSupplier ASC, fBarcode ASC");
            proc.Run(getProd);
        }
        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {

                //MessageBox.Show(desc, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show("An error occured while exporting products data."+
                "Please check the application settings.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            });
        }
        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "GETPROD":
                        prog = 40f;
                        Progress();
                        UnityScript saveProd;
                        proc.Data_Fetch(out saveProd);
                        saveProd.Script_Clear();
                        saveProd.Script_Add(Script.PROC_NAME, "SAVEPROD");
                        saveProd.Script_Add(Script.LFFPU_WRITE, _filename, "elayorbulc");
                        saveProd.Desc_FileTitle = _filename;
                        saveProd.Desc_MetaData = "MBB Products Table";
                        proc.Run(saveProd);
                        break;
                    case "SAVEPROD":
                        prog = 100f;
                        Progress();
                        tmr.Enabled = true;
                        break;
                }
            });
        }
        void Progress()
        {
            lblProgress.Text = prog.ToString("#,#0.#") + "%";
            prgBar.Value = (int)prog;
        }
    }
}
